package com.bootdo.front.service.impl;

import com.bootdo.front.dao.*;
import com.bootdo.front.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.front.service.ResumeService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ResumeServiceImpl implements ResumeService {
	@Autowired
	private ResumeDao resumeDao;
	@Autowired
	private ProjectExperienceDao projectExperienceDao;

	@Autowired
	private WorkExperienceDao workExperienceDao;

	@Autowired
	private EducationDao educationDao;

	@Autowired
	private UserResumeDao userResumeDao;

	@Autowired
	private ResumeProjectDao resumeProjectDao;

	@Autowired
	private ResumeWorkDao resumeWorkDao;

	@Override
	public ResumeDO get(Long id){
		return resumeDao.get(id);
	}

	@Override
	public List<ResumeDO> getByUserId(long userid) {
		return resumeDao.getByUserId(userid);
	}


	@Override
	public List<ResumeDO> list(Map<String, Object> map){
		return resumeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return resumeDao.count(map);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int save(Long userId ,ResumeDO resume){
		resume.setCreateTime(new Date());
		resume.setUpdateTime(new Date());
		resumeDao.save(resume);
		Long resumeId = resume.getId();

		UserResumeDO userResume = new UserResumeDO();
		userResume.setUserid(userId);
		userResume.setResume(resumeId);
		userResumeDao.save(userResume);

		List<EducationDO> educations = resume.getEducations();
		if(educations != null && !educations.isEmpty()){
			for(EducationDO ed : educations){
				ed.setResumeId(resumeId);
				educationDao.save(ed);
			}
		}

		List<ProjectExperienceDO> projectExperiences = resume.getProjectExperiences();
		if(projectExperiences != null && !projectExperiences.isEmpty()){
			for(ProjectExperienceDO projectExperience : projectExperiences){

				projectExperience.setCreateTime(new Date());
				projectExperience.setUpdateTime(new Date());

				projectExperienceDao.save(projectExperience);
				Long projectExperienceId = projectExperience.getId();

				ResumeProjectDO resumeProjectDO = new ResumeProjectDO();
				resumeProjectDO.setProjectid(projectExperienceId);
				resumeProjectDO.setResumeid(resumeId);
				resumeProjectDao.save(resumeProjectDO);
			}
		}

		List<WorkExperienceDO> workExperiences = resume.getWorkExperiences();
		if(workExperiences != null && !workExperiences.isEmpty()){
			for(WorkExperienceDO workExperience : workExperiences){

				workExperience.setCreateTime(new Date());
				workExperience.setUpdateTime(new Date());

				workExperienceDao.save(workExperience);
				Long workExperienceId = workExperience.getId();

				ResumeWorkDO resumeWorkDO = new ResumeWorkDO();
				resumeWorkDO.setResumeid(resumeId);
				resumeWorkDO.setWorkid(workExperienceId);
				resumeWorkDao.save(resumeWorkDO);
			}
		}

		return 1;
	}

	@Transactional
	@Override
	public int update(Long userId, ResumeDO resume){
		resumeDao.update(resume);
		Long resumeId = resume.getId();

		//删除原先的工作经验，项目经验记录
		resumeProjectDao.remove(resumeId);
		resumeWorkDao.remove(resumeId);

		projectExperienceDao.removeByResumeId(resumeId);
		workExperienceDao.removeByResumeId(resumeId);
		educationDao.removeByResumeId(resumeId);

		List<EducationDO> educations = resume.getEducations();
		if(educations != null && educations.size()>0){
			for(EducationDO ed : educations){
				ed.setResumeId(resumeId);
				educationDao.save(ed);
			}
		}

		List<ProjectExperienceDO> projectExperiences = resume.getProjectExperiences();
		if(projectExperiences != null && !projectExperiences.isEmpty()){
			for(ProjectExperienceDO projectExperience : projectExperiences){
				projectExperienceDao.save(projectExperience);
				Long projectExperienceId = projectExperience.getId();

				ResumeProjectDO resumeProjectDO = new ResumeProjectDO();
				resumeProjectDO.setProjectid(projectExperienceId);
				resumeProjectDO.setResumeid(resumeId);
				resumeProjectDao.save(resumeProjectDO);
			}
		}

		List<WorkExperienceDO> workExperiences = resume.getWorkExperiences();
		if(workExperiences != null && !workExperiences.isEmpty()){
			for(WorkExperienceDO workExperience : workExperiences){
				workExperienceDao.save(workExperience);
				Long workExperienceId = workExperience.getId();

				ResumeWorkDO resumeWorkDO = new ResumeWorkDO();
				resumeWorkDO.setResumeid(resumeId);
				resumeWorkDO.setWorkid(workExperienceId);
				resumeWorkDao.save(resumeWorkDO);
			}
		}

		return 1;
	}

	@Transactional
	@Override
	public int remove(Long resumeId){
		resumeDao.remove(resumeId);

		//删除原先的工作经验，项目经验记录
		resumeProjectDao.remove(resumeId);
		resumeWorkDao.remove(resumeId);

		projectExperienceDao.removeByResumeId(resumeId);
		workExperienceDao.removeByResumeId(resumeId);
		educationDao.removeByResumeId(resumeId);
		return 1;
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return resumeDao.batchRemove(ids);
	}

	@Override
	public int save(ResumeDO resume){
		return resumeDao.save(resume);
	}

	@Override
	public int update(ResumeDO resume){
		return resumeDao.update(resume);
	}

	@Override
	public ResumeDO getUserinfo(Long id) {
	return 	resumeDao.getUserinfo(id);

	}

}
